/*
 * BSM6.0
 *
 * Copyright 2012-2013 (C) SINODATA CO., LTD. All Rights Reserved.
 */
package com.sinodata.bsm.center.web.action.performance;

import java.text.SimpleDateFormat;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.util.CellRangeAddress;
import org.springframework.beans.factory.annotation.Autowired;

import com.sinodata.bsm.center.bean.ResBean;
import com.sinodata.bsm.center.cache.PropertyCache;
import com.sinodata.bsm.center.service.performance.PropValueService;
import com.sinodata.bsm.center.web.action.BaseExportAction;
import com.sinodata.bsm.common.vo.Property;
import com.sinodata.bsm.common.vo.PropertyValue;

/**
 * <p>
 * Description: 性能数据导出
 * </p>
 *
 * @author wangyazhou
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2013-2-21 下午1:22:17          wangyazhou        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class PropertyExportAction extends BaseExportAction<PropertyValue> {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd HH:mm");

    @Autowired
    private PropertyCache propertyCache;

    @Autowired
    private PropValueService propService;

    /**
     * 导出原始性能数据
     */
    @Override
    public String doExport() throws Exception {
        //------------查询条件-------------------------------------
        String resId = request.getParameter("resId");
        String dimen = request.getParameter("dimen");
        String propIds = request.getParameter("propIds");
        String timeSection = request.getParameter("timeSection");
        String beginDate = request.getParameter("beginDate");
        String endDate = request.getParameter("endDate");

        String[] props = propIds.split("[,]");

        int[] propids = new int[props.length];
        for (int i = 0; i < props.length; i++) {
            Property p = propertyCache.get(Long.parseLong(props[i]));
            int ii = p.getStore();
            propids[i] = ii;
        }

        if (dimen == null || dimen.length() == 0) {
            dimen = "0";
        }

        //-----------文件名-----------------------------------------
        StringBuilder sb = new StringBuilder("");
        ResBean bean = ResBean.get(Long.parseLong(resId));
        if (bean != null) {
            sb.append(bean.res().getName() + "_性能_");
        } else {
            sb.append("性能_");
        }
        if ("0".equals(dimen)) {
            sb.append("原始");
        } else if ("1".equals(dimen)) {
            sb.append("时汇聚");
        } else {
            sb.append("日汇聚");
        }
        String displayFileName = new String(sb.toString().getBytes("GBK"), "iso-8859-1");
        response.setContentType("APPLICATION/msexcel; charset=utf-8");
        response.addHeader("Content-Disposition", "attachment;filename=\"" + displayFileName.trim().replaceAll("\\s", "") + ".xls\"");

        //------------sheet名称-------------------
        HSSFSheet sheet = workBook.createSheet("性能数据列表");

        //-----------性能数据-----------------------
        List<PropertyValue> list = propService.getPropValues(Long.parseLong(resId), propids, DateGeneric.genericStartDate(timeSection, beginDate), DateGeneric.getnericEndDate(dimen, timeSection, endDate), dimen);

        if ("0".equals(dimen)) { //原始性能数据
            //-------------列宽-------------------
            for (int i = 0; i < props.length + 1; i++) {
                sheet.setColumnWidth(i, 6000);
            }

            //-------------表头-------------------------
            HSSFCellStyle style = getStyle(STYLE_HEADING);
            HSSFRow rowHead = sheet.createRow(0);

            HSSFCell timeHead = rowHead.createCell(0);
            timeHead.setCellValue(new HSSFRichTextString("采集时间"));
            timeHead.setCellStyle(style);

            for (int j = 0; j < props.length; j++) {
                HSSFCell cell = rowHead.createCell(j + 1);
                cell.setCellValue(new HSSFRichTextString(propertyCache.get(Long.parseLong(props[j])).getName()));
                cell.setCellStyle(style);
            }

            //------------表体赋值-----------------------
            style = getStyle(STYLE_CONTENT);
            int rowNum = 1;
            for (int k = 0; k < Math.min(list.size(), 60000); k++) {
                PropertyValue pv = list.get(k);
                HSSFRow row = sheet.createRow(rowNum++);
                boolean odd = rowNum % 2 != 0;

                HSSFCell time = row.createCell(0);
                time.setCellValue(new HSSFRichTextString(simple.format(pv.getTime())));
                if (odd) {
                    time.setCellStyle(style);
                }
                String[] values = pv.getValues();
                for (int l = 0; l < props.length; l++) {
                    HSSFCell cell = row.createCell(l + 1);
                    cell.setCellValue(new HSSFRichTextString(values[l]));
                    if (odd) {
                        cell.setCellStyle(style);
                    }
                }
            }
        } else { //汇聚性能数据
            //----------列宽------------------------
            for (int i = 0; i < props.length * 3 + 1; i++) {
                if (i == 0) {
                    sheet.setColumnWidth(i, 7000);
                } else {
                    sheet.setColumnWidth(i, 3000);
                }
            }

            //----------合并第一行---------------------------
            for (int i = 0; i < props.length; i++) {
                sheet.addMergedRegion(new CellRangeAddress(0, 0, i * 3 + 1, i * 3 + 3));
            }
            //-------------第一行表头-------------------------
            HSSFCellStyle style = getStyle(STYLE_HEADING);
            HSSFRow firstHead = sheet.createRow(0);

            HSSFCell timeHead = firstHead.createCell(0);
            timeHead.setCellValue(new HSSFRichTextString(""));
            timeHead.setCellStyle(style);

            for (int j = 0; j < props.length; j++) {
                HSSFCell cell = firstHead.createCell(j * 3 + 1);
                cell.setCellValue(new HSSFRichTextString(propertyCache.get(Long.parseLong(props[j])).getName()));
                cell.setCellStyle(style);
            }
            //-------------第二行表头--------------------------
            HSSFRow secodeHead = sheet.createRow(1);

            HSSFCell stimeHead = secodeHead.createCell(0);
            stimeHead.setCellValue(new HSSFRichTextString("采集时间"));
            stimeHead.setCellStyle(style);
            for (int j = 0; j < props.length; j++) {

                HSSFCell head1 = secodeHead.createCell(j * 3 + 1);
                head1.setCellValue(new HSSFRichTextString("平均值"));
                head1.setCellStyle(style);

                HSSFCell head2 = secodeHead.createCell(j * 3 + 2);
                head2.setCellValue(new HSSFRichTextString("最大值"));
                head2.setCellStyle(style);

                HSSFCell head3 = secodeHead.createCell(j * 3 + 3);
                head3.setCellValue(new HSSFRichTextString("最小值"));
                head3.setCellStyle(style);
            }

            //----------------表体赋值--------------------------
            style = getStyle(STYLE_CONTENT);
            int rowNum = 2;
            for (int k = 0; k < Math.min(list.size(), 60000); k++) {
                PropertyValue pv = list.get(k);
                HSSFRow row = sheet.createRow(rowNum++);
                boolean odd = rowNum % 2 == 0;

                HSSFCell time = row.createCell(0);
                time.setCellValue(new HSSFRichTextString(simple.format(pv.getTime())));
                if (odd) {
                    time.setCellStyle(style);
                }
                for (int kk = 0; kk < props.length; kk++) {
                    HSSFCell cell1 = row.createCell(kk * 3 + 1);
                    cell1.setCellValue(new HSSFRichTextString(pv.getValues()[4 * kk]));
                    cell1.setCellStyle(style);
                    HSSFCell cell2 = row.createCell(kk * 3 + 2);
                    cell2.setCellValue(new HSSFRichTextString(pv.getValues()[4 * kk + 2]));
                    cell2.setCellStyle(style);
                    HSSFCell cell3 = row.createCell(kk * 3 + 3);
                    cell3.setCellValue(new HSSFRichTextString(pv.getValues()[4 * kk + 3]));
                    cell3.setCellStyle(style);
                    if (odd) {
                        cell1.setCellStyle(style);
                        cell2.setCellStyle(style);
                        cell3.setCellStyle(style);
                    }
                }
            }
        }

        //--------------导出-------------
        workBook.write(response.getOutputStream());
        return null;
    }

    @Override
    public String input() throws Exception {
        return null;
    }

}
